package com.smarthospital.web.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.smarthospital.common.Result.ResultDTO;
import com.smarthospital.entity.dto.LoginResultDTO;
import com.smarthospital.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * create by sym  2020/8/16 16:00.
 */
@RestController
@Slf4j
@RequestMapping("/weChat")
@Api(tags = "微信相关接口",value = "weChat")
public class WXController {

    @Autowired
    private LoginService loginService;

    @PostMapping("/login")
    @ApiOperation(value = "登录接口",
            notes =" 1.微信登录回调接口，支持直接传 code\n " +
                    "2.服务器去微信拿oppenId unionId\n" +
                    "3.服务器查询用户表，如果未注册，会执行初始化注册\n" +
                    "4.服务器发token\n" +
                    "param: code -> 微信登录code; source -> 请求来源 0_小程序(不传默认0) 1_公众号\n" )
    public ResultDTO login(@RequestBody JSONObject param){
        String code = param.getString("code");
        Integer source = param.getInteger("source");
        log.info("getUser code {}  source {}",code,source );
        LoginResultDTO resultDTO = loginService.login(source,code);
        log.info("【测试  token】 {}", JSON.toJSONString(resultDTO));
        return ResultDTO.sucess(resultDTO);
    }


}
